package cn.otrue.patienthealthmanager.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.utils.NetCheckUtil;

import com.google.gson.reflect.TypeToken;

/**
 * 找回密码
 * 
 * @author G
 * 
 */
public class PasswordBackActivity extends BaseActivity {
	private TextView tv_title_back;
	private TextView tv_title_title;
	private EditText et_password_inpute;
	private EditText et_password_back_yz;
	private Button btn_password_yz;
	private Button btn_passwordback_next;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_password_back);
		initView();

	}

	private void initView() {
		tv_title_title = (TextView) findViewById(R.id.tv_title_title);
		tv_title_title.setText(getString(R.string.password_back));
		tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(this);
		et_password_inpute = (EditText) findViewById(R.id.et_password_inpute);
		et_password_back_yz = (EditText) findViewById(R.id.et_password_back_yz);
		btn_password_yz = (Button) findViewById(R.id.btn_password_yz);
		btn_password_yz.setOnClickListener(this);
		btn_passwordback_next = (Button) findViewById(R.id.btn_passwordback_next);
		btn_passwordback_next.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_title_back:
			finish();
			break;
		case R.id.btn_password_yz:
			btn_passwordback_next.setClickable(true);
			if (NetCheckUtil.checkNet(PasswordBackActivity.this)) {
				if (TextUtils.isEmpty(et_password_inpute.getText().toString())) {
					Toast.makeText(getApplicationContext(), MyConst.DENG_LU, 0)
							.show();
				} else if ((et_password_inpute.getText().toString().length() != 11)) {
					Toast.makeText(getApplicationContext(), MyConst.YONGHUMING,
							0).show();
				} else {
					yanzheng();
				}
			} else {
				Toast.makeText(PasswordBackActivity.this,
						getString(R.string.network_problems), Toast.LENGTH_LONG)
						.show();
			}
			btn_password_yz.setClickable(false);
			break;
		case R.id.btn_passwordback_next:
			if (NetCheckUtil.checkNet(PasswordBackActivity.this)) {
				btn_passwordback_next.setClickable(false);
				if (TextUtils.isEmpty(et_password_back_yz.getText().toString())) {
					btn_passwordback_next.setClickable(true);
					Toast.makeText(getApplicationContext(), "验证码不能为空", 0)
							.show();
				} else if (TextUtils.isEmpty(et_password_inpute.getText()
						.toString().trim())) {
					btn_passwordback_next.setClickable(true);
					Toast.makeText(getApplicationContext(), MyConst.DENG_LU, 0)
							.show();
				} else if ((et_password_inpute.getText().toString().length() != 11)
						&& !(et_password_inpute.getText().toString().trim()
								.matches("[1][358][0-9]{9}"))) {
					btn_passwordback_next.setClickable(true);
					Toast.makeText(getApplicationContext(), MyConst.YONGHUMING,
							0).show();
				} else {
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("aliasAccount", "p_"
							+ et_password_inpute.getText().toString());
					map.put("mobileVerifyCode", et_password_back_yz.getText()
							.toString());
					TransferParameter parameter = new TransferParameter();
					parameter.setData(map);
					new HttpServer().post(HttpCons.captchaVerifieds, parameter,
							ReHandler);
				}
			} else {
				Toast.makeText(PasswordBackActivity.this,
						getString(R.string.network_problems), Toast.LENGTH_LONG)
						.show();
			}
			break;
		}
	}

	/**
	 * @param aliasAccount用户别名
	 * @param messageType
	 *            1代表忘记密码 0代表注册 获取验证码
	 */
	private void yanzheng() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("mobileaccount", "p_" + et_password_inpute.getText().toString());
		map.put("messageType", "1");
		TransferParameter parameter = new TransferParameter();
		parameter.setData(map);
		new HttpServer().post(HttpCons.MobileMessageVerify, parameter, Handler);
	}

	private Handler Handler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			if (state.equals("001")) {
				// String d= data.getString(HttpCons.DATA);//返回的data数据json
				new Thread(new Runnable() {

					@Override
					public void run() {

						for (int i = 60; i > 0; i--) {
							Message msg = new Message();
							msg.what = 0;
							msg.arg1 = i;
							handlerTime.sendMessage(msg);
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}

						}
						Message msg = new Message();
						msg.what = 1;
						handlerTime.sendMessage(msg);
					}
				}).start();
				Toast.makeText(getApplicationContext(), msg, 0).show();
			} else {
				btn_password_yz.setClickable(true);
				Toast.makeText(getApplicationContext(), msg, 0).show();

			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(PasswordBackActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
	private Handler handlerTime = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				btn_password_yz.setText("(" + msg.arg1 + "s)重新获取");

				btn_password_yz.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {

					}
				});
				btn_password_yz.setClickable(false);
				btn_password_yz.setBackgroundResource(R.drawable.shape_button);
				break;
			case 1:
				btn_password_yz.setText("获取验证码");
				btn_password_yz.setOnClickListener(PasswordBackActivity.this);
				btn_password_yz.setClickable(true);
				btn_password_yz
						.setBackgroundResource(R.drawable.shape_button_auxiliary_normal);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	private Handler ReHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			if (state.equals("001")) {
				String dataStr = data.getString(HttpCons.DATA);
				Map<String, String> map = HttpServer.gson.fromJson(dataStr,
						new TypeToken<Map<String, String>>() {
						}.getType());
				Intent mianIntent = new Intent(PasswordBackActivity.this,
						PasswordChangeActivity.class);
				mianIntent.putExtra("from", "passwordback");
				mianIntent.putExtra("token", map.get("token"));
				mianIntent.putExtra("verification_code", map.get("code"));
				mianIntent.putExtra("phone", et_password_inpute.getText()
						.toString().trim());
				startActivity(mianIntent);
				finish();
				Toast.makeText(getApplicationContext(), msg, 0).show();
			} else {
				btn_passwordback_next.setClickable(true);
				Toast.makeText(getApplicationContext(), msg, 0).show();
			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			btn_passwordback_next.setClickable(true);
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(PasswordBackActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};
}
